package br.ufpe.cin.analysis.plugin.simulation.entities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.jfree.data.general.Dataset;

public abstract class SimulationDataset<T extends Dataset> {

	private SimulationData simulationData;
	private T dataset;
	private File xmlFile;

	// Receives a xml file
	public SimulationDataset(String filename) throws FileNotFoundException {
		this.xmlFile = new File(filename);
		this.simulationData = new SimulationData();
	}

	public SimulationData getSimulationData() {
		return this.simulationData;
	}

	public void setSimulationData(SimulationData simulationData) {
		this.simulationData = simulationData;
	}

	public T getDataset() {
		return this.dataset;
	}

	public void setDataset(T dataset) {
		this.dataset = dataset;
	}

	public void loadProcess() throws FileNotFoundException, JAXBException {
		FileInputStream in = new FileInputStream(this.xmlFile);

		JAXBContext context = JAXBContext.newInstance(Process.class);

		Unmarshaller unmarshaller = context.createUnmarshaller();

		this.simulationData = (SimulationData) unmarshaller.unmarshal(in);

	}

	public void storeProcess() throws JAXBException, IOException {

		FileOutputStream out = new FileOutputStream(this.xmlFile);

		JAXBContext context = JAXBContext.newInstance(Process.class);

		Marshaller marshaller = context.createMarshaller();

		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

		marshaller.marshal(this.simulationData, out);

		out.close();

	}

	public abstract void createDataset();
}